package com.eedi.framework.logger.convert;

import cn.hutool.core.util.ObjectUtil;
import com.eedi.framework.apilog.core.service.ApiAccessLog;
import com.eedi.framework.apilog.core.service.ApiErrorLog;
import com.eedi.framework.common.util.collection.CollectionUtils;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.logger.controller.sys.vo.operatelog.SysOperateLogCreateReq;
import com.eedi.framework.logger.controller.sys.vo.operatelog.SysOperateLogResp;
import com.eedi.framework.logger.dal.dataobject.SysLogOperateDO;
import com.eedi.framework.operatelog.core.enums.OperateTypeEnum;
import com.eedi.framework.operatelog.core.service.OperateLog;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface SysOperateLogConvert {

    SysOperateLogConvert INSTANCE = Mappers.getMapper(SysOperateLogConvert.class);

    default List<SysOperateLogResp> convertList(List<SysLogOperateDO> list) {
        return CollectionUtils.convertList(list, log -> {
            SysOperateLogResp logVO = BeanUtils.toBean(log, SysOperateLogResp.class);
            logVO.setName(log.getOptName());
            logVO.setOptType(log.getOptType());
            return logVO;
        });
    }

    default SysOperateLogResp convert(SysLogOperateDO orgLogOperateDO) {
        if (ObjectUtil.isEmpty(orgLogOperateDO)) {
            return null;
        }
        SysOperateLogResp logVO = BeanUtils.toBean(orgLogOperateDO, SysOperateLogResp.class);
        logVO.setName(orgLogOperateDO.getOptName());
        logVO.setOptType(orgLogOperateDO.getOptType());
        return logVO;
    }

    default SysOperateLogCreateReq convert(OperateLog api) {
        if (api == null) {
            return null;
        }
        SysOperateLogCreateReq req = new SysOperateLogCreateReq();
        req.setUserId(api.getUserId())
                .setUserType(api.getUserType())
                .setOrgInfoId(api.getOrgInfoId())
                .setOptName(api.getName())
                .setModule(api.getModule())
                .setOptContent(api.getJavaMethodArgs())
                .setReqMethod(api.getRequestMethod())
                .setReqUrl(api.getRequestUrl())
                .setUserIp(api.getUserIp())
                .setUserAgent(api.getUserAgent())
                .setStartTime(api.getStartTime())
                .setDuration(api.getDuration())
                .setResultCode(api.getResultCode())
                .setResultMsg(api.getResultMsg());
        return req;

    }
    default SysOperateLogCreateReq convert(ApiAccessLog api) {
        if (api == null) {
            return null;
        }
        SysOperateLogCreateReq req = new SysOperateLogCreateReq();
        req.setUserId(api.getUserId())
                .setUserType(api.getUserType())
                .setOptType(OperateTypeEnum.get(api.getRequestMethod()))
                .setOptContent(api.getRequestParams())
                .setOrgInfoId(api.getOrgInfoId())
                .setReqMethod(api.getRequestMethod())
                .setReqUrl(api.getRequestUrl())
                .setUserIp(api.getUserIp())
                .setModule(api.getModule())
                .setUserAgent(api.getUserAgent())
                .setStartTime(api.getBeginTime())
                .setDuration(api.getDuration())
                .setJavaMethodArgs(api.getRequestParams())
                .setResultCode(api.getResultCode())
                .setOptName(api.getOptName())
                .setResultMsg(api.getResultMsg());
        return req;

    }

    default SysOperateLogCreateReq convert(ApiErrorLog api) {
        if (api == null) {
            return null;
        }
        SysOperateLogCreateReq req = new SysOperateLogCreateReq();
        req.setUserId(api.getUserId())
                .setUserType(api.getUserType())
                .setOptType(OperateTypeEnum.CREATE)
                .setOptContent(api.getRequestParams())
                .setReqMethod(api.getRequestMethod())
                .setReqUrl(api.getRequestUrl())
                .setUserIp(api.getUserIp())
                .setUserAgent(api.getUserAgent());
//                .setStartTime(api.getBeginTime())
//                .setDuration(api.getDuration())
//                .setResultCode(api.getResultCode())
//                .setResultMsg(api.getResultMsg());
        return req;

    }

}
